﻿app.controller("FireControlController", function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.GetImportantList = function (model,page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../FC/GetImportantList',
            data: {'model':model, 'page': page}
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.DataList = ret.Data.Data;
                $scope.TotalCount = ret.Data.TotalItemCount;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.BindDepartmentList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../SystemConfig/GetDepartmentList'
        })
            .success(function (ret) {
                $scope.PageLoading = false;
                common.success($scope, null, ret);
                if (ret.Code == 100) {

                    $.each(ret.Data, function (i, n) {
                        if (n.DepartmentName != "系统支持") {
                            if (defaultvalue != null) {
                                if (n.Id == defaultvalue)
                                    $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                                else
                                    $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                            else {
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.GetFactoryList = function () {
        $scope.FactoryList = [];
        $http({
            method: 'POST',
            url: '../EmergencyPlan/GetFactoryList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.FactoryList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }



    $scope.GetTraningPageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../FC/GetTraningPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.DataList = ret.Data.Data;
                $scope.TotalCount = ret.Data.TotalItemCount;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.AddTrainingInfo = function (model, searchmodel) {
        $scope.DataLoading = true;
        var files = document.querySelector('input[type=file]').files;
        Upload.upload({
            method: 'POST',
            url: '../FC/AddTrainingInfo',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                alertify.alert("保存成功！");
                $("#btn_close").click();
                $scope.GetTraningPageList(searchmodel,1);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })

    }

    $scope.AddSupplies = function (model,searchmodel) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../FC/AddSupplies',
            data: { 'model': model }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    alertify.alert("保存成功！");
                    $("#btn_close").click();
                    $scope.GetSuppliesPageList(searchmodel, 1);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })

    }

    $scope.GetFighterPageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../FC/GetFighterPageList',
            data: { 'Factory': model, 'page': page }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.DataList = ret.Data.Data;
                $scope.TotalCount = ret.Data.TotalItemCount;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }


    $scope.GetSuppliesPageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../FC/GetSuppliesPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.DataList = ret.Data.Data;
                $scope.TotalCount = ret.Data.TotalItemCount;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetPatrolItemList = function () {
        $scope.OptionList = [];
        $http({
            method: 'POST',
            url: '../FC/GetPatrolItemList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.OptionList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetPatrolPageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../FC/GetPatrolPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.DataList = ret.Data.Data;
                $scope.TotalCount = ret.Data.TotalItemCount;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetTagPageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../FC/GetIconPageList',
            data: { 'page': page }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.DataList = ret.Data.Data;
                $scope.TotalCount = ret.Data.TotalItemCount;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.ShowImportantDetail = function (model) {
        $scope.Detail = model;

    }
    $scope.InitAddForm = function () {
        $scope.AddModel = {};
        $("#filecontrol").empty();
        $("#filecontrol").append(' <input type="file" ngf-select  name="docfiles" id="docfiles" class="file" initialPreviewAsData="true" data-show-remove="true" data-show-preview="true" showCancel="false" data-show-upload="false" required>');
        $('#docfiles').fileinput({
            language: 'zh'
        });
    }
    $scope.AddFighter = function (model) {
        if (model.FactoryArea == null || model.FactoryArea == "") {
            alertify.alert("请选择厂区！");
            return false;
        }
        if (model.Name == null || model.Name == "") {
            alertify.alert("请输入姓名！");
            return false;
        }
        $scope.DataLoading = true;
        var files = document.querySelector('input[type=file]').files;
        Upload.upload({
            method: 'POST',
            url: '../FC/AddFighter',
            data: { 'model': model },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                alertify.alert("保存成功！");
                $scope.GetFighterPageList(null, 1);
                $("#btn_close").click();
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.ShowFighter = function (model) {
        $scope.Detail = model;
        $http({
            method: 'POST',
            url: '../FC/GetFighterFileList',
            data: { 'Id': model.Id }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.FileList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.ShowTraingingForm = function (model) {
        $scope.Detail = model;
        $http({
            method: 'POST',
            url: '../FC/GetTraningFileList',
            data: { 'Id': model.Id }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.FileList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.DeleteFighter = function (model, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除数据？", function (ev) {
                ev.preventDefault();
                $scope.DataLoading = true;
                $http({
                    method: 'POST',
                    url: '../FC/DeleteFighter',
                    data: { 'model': model }
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert("操作成功！");
                            $scope.GetFighterPageList(searchmodel, page);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        $scope.DataLoading = false;
                        common.error($scope);
                    })
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.DeleteTraingForm = function (model, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除数据？", function (ev) {
                ev.preventDefault();
                $scope.DataLoading = true;
                $http({
                    method: 'POST',
                    url: '../FC/DeleteTrainingForm',
                    data: { 'model': model }
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert("操作成功！");
                            $scope.GetTraningPageList(searchmodel, page);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        $scope.DataLoading = false;
                        common.error($scope);
                    })
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.DeleteSupplies = function (model, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除数据？", function (ev) {
                ev.preventDefault();
                $scope.DataLoading = true;
                $http({
                    method: 'POST',
                    url: '../FC/DeleteSupplies',
                    data: { 'model': model }
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert("操作成功！");
                            $scope.GetSuppliesPageList(searchmodel, page);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        $scope.DataLoading = false;
                        common.error($scope);
                    })
            }, function (ev) {
                ev.preventDefault();
            });
    }
    $scope.ShowSupplies = function (model) {
        $scope.Detail = model;
       
    }
});